package com.poker.service;

import com.poker.entity.*;
import com.poker.repository.*;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName UserGameService
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/18 15:41
 * @Version 1.0
 **/
@Service
public class UserGameService {
    @Resource
    private UserGameRepository userGameRepository;

    @Resource
    private ActivityRepository activityRepository;

    @Resource
    private UserRepository userRepository;

    @Resource
    private UserPointsConsumeRepository userPointsConsumeRepository;

    @Resource
    private UserPointsGainRepository userPointsGainRepository;



    public void saveUserGame(UserGame userGame) {
        Activity one = activityRepository.getOne(userGame.getActivityId());
        if(userGame.getGameStatus()==1){
            UserPointsConsume userPointsConsume=new UserPointsConsume();
            userPointsConsume.setUserId(userGame.getUserId());
            userPointsConsume.setConsumeDirection("活动比赛");
            userPointsConsume.setConsumeExplain("在"+one.getActivityName()+"比赛中"+userGame.getWhatScene()+"输掉了"+userGame.getIntegral()+"积分");
            userPointsConsume.setConsumeDate(new Date());
            userPointsConsume.setPointsCount(userGame.getIntegral());
            userPointsConsumeRepository.saveAndFlush(userPointsConsume);
            User user = userRepository.getOne(userGame.getUserId());
            if(user.getIntegral()!=null){
                user.setIntegral(user.getIntegral() - userGame.getIntegral());
            }
            if(one.getActivityName().equals("德州扑克")){
                user.setIntegralD(user.getIntegralD()- userGame.getIntegral());
            }
            if(one.getActivityName().equals("双升扑克")){
                user.setIntegralS(user.getIntegralS()-userGame.getIntegral());
            }

        }
        if(userGame.getGameStatus()==0){
            UserPointsGain userPointsGain=new UserPointsGain();
            userPointsGain.setUserId(userGame.getUserId());
            userPointsGain.setGainDate(new Date());
            userPointsGain.setGainSource("活动比赛");
            userPointsGain.setGainExplain("在"+one.getActivityName()+"比赛中"+userGame.getWhatScene()+"赢取了"+userGame.getIntegral()+"积分");
            userPointsGain.setPointsCount(userGame.getIntegral());
            userPointsGainRepository.saveAndFlush(userPointsGain);
            User user = userRepository.getOne(userGame.getUserId());
            if(user.getIntegral()!=null){
                user.setIntegral(user.getIntegral()+ userGame.getIntegral());
            }else{
                user.setIntegral(userGame.getIntegral());
            }
            if(one.getActivityName().equals("德州扑克")){
                user.setIntegralD(user.getIntegralD() + userGame.getIntegral());
            }
            if(one.getActivityName().equals("双升扑克")){
                user.setIntegralS(user.getIntegralS() + userGame.getIntegral());
            }
            userRepository.saveAndFlush(user);
        }
        userGame.setCreatDate(new Date());
        userGameRepository.saveAndFlush(userGame);
    }
}
